<template>
  <el-drawer :title="title" :visible.sync="visible" append-to-body class="drawer-container" size="400px">
    <el-form ref="form" :model="formdata" :rules="formrule" label-position="right" label-suffix=":" label-width="100px">
      <el-form-item label="岗位类型" prop="type">
        <el-select v-model="formdata.type" placeholder="请选择岗位类型" style="width: 100%">
          <el-option v-for="item in localDict.post_type" :key="item.value" :label="item.label" :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="岗位名称" prop="name">
        <el-input v-model="formdata.name" placeholder="请输入岗位名称"/>
      </el-form-item>
      <el-form-item label="岗位编码" prop="code">
        <el-input v-model="formdata.code" placeholder="请输入编码名称"/>
      </el-form-item>
      <el-form-item label="岗位顺序" prop="sort">
        <el-input-number v-model="formdata.sort" :min="0" controls-position="right"/>
      </el-form-item>
      <el-form-item label="岗位状态" prop="status">
        <el-radio-group v-model="formdata.status">
          <el-radio-button v-for="item in localDict.post_status" :key="item.value" :label="item.value">
            {{ item.label }}
          </el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formdata.remark" placeholder="请输入内容" type="textarea"/>
      </el-form-item>
    </el-form>
    <div class="drawer-footer">
      <el-button size="mini" :loading="loading" type="primary" @click="handleSubmit">确 定</el-button>
      <el-button size="mini" @click="handleCancel">取 消</el-button>
    </div>
  </el-drawer>
</template>

<script>

import {getOne, save} from "@/api/base";

export default {
  name: "PostModel",
  components: {},
  data() {
    return {
      title: '',
      visible: false,
      loading: false,
      baseUrl: '/system/post',
      formdata: {},
      formrule: {
        // 表单校验
        code: [
          {required: true, message: "岗位编码不能为空", trigger: "blur"},
          {pattern: /^\w{2,30}$/, message: '客户端编码只能包含数字、字母、下划线,长度在2-30个字符', trigger: 'blur'},
        ],
        name: [
          {required: true, message: "岗位名称不能为空", trigger: "blur"}
        ],
        type: [
          {required: true, message: "岗位类型不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: "岗位状态不能为空", trigger: "blur"}
        ]
      },
      localDict: {
        post_type: [],
        post_status: [],
      },
    }
  },
  created() {
    this.getLocalDict();
  },
  methods: {
    add() {
      this.resetForm('form')
      this.formdata = {
        status: '00'
      }
      this.visible = true
      this.title = '新增岗位'
    },
    edit(id) {
      this.resetForm('form')
      getOne(this.baseUrl, id).then(res => {
        this.formdata = res.data
        this.visible = true
        this.title = '修改岗位'
      })
    },
    async getLocalDict() {
      await this.getStaticData('system-post-type').then(res => {
        this.localDict.post_type = res.data
      })
      await this.getStaticData('system-status').then(res => {
        this.localDict.post_status = res.data
      })
    },
    handleSubmit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true
          save(this.baseUrl, this.formdata).then(() => {
            this.$modal.msgSuccess("保存成功");
            this.visible = false;
            this.resetForm('form')
            this.$emit('ok')
          }).finally(() => {
            this.loading = false
          });
        }
      });
    },
    handleCancel() {
      this.visible = false
      this.resetForm('form')
      this.$emit('close')
    }
  }
}
</script>
<style lang="scss" scoped>

</style>
